<?php
/**
*
* 功能描述（管理员控制器）
*
* @author suoya <jlusuoya@gmail.com>
* @version 1
*/

class CtrlAdmin extends CtrlBase
{
	public $paramCheck = array(
		"showlist" => array(
			"page" => array("GET", "1", "intval", "positive"),
		),
		"setpwd" => array(
			"id" => array("GET", "must", "intval", "positive"),
		),
		"info" => array(
			"id" => array("GET", "must", "intval", "positive"),
		),
		"add" => array(
			"username" => array("POST", "must", "trim", "notempty"),
			"password" => array("POST", "must", "trim", "notempty"),
			"mobile" => array("POST", "must", "trim", "mobile"),
			"email" => array("POST", "must", "trim", "email"),
			"truename" => array("POST", "must", "trim", "notempty"),
			"desp" => array("POST", "must", "trim", "notempty"),
			"cake" => array("POST", "0", "intval", "bool"),
			"order" => array("POST", "0", "intval", "bool"),
			"prod" => array("POST", "0", "intval", "bool"),
			"dely" => array("POST", "0", "intval", "bool"),
			"msg" => array("POST", "0", "intval", "bool"),
			"user" => array("POST", "0", "intval", "bool"),
			"log" => array("POST", "0", "intval", "bool"),
			"adm" => array("POST", "0", "intval", "bool"),
		),
		"setperm" => array(
			"id" => array("GET", "must", "intval", "positive"),
			"cake" => array("POST", "0", "intval", "bool"),
			"order" => array("POST", "0", "intval", "bool"),
			"prod" => array("POST", "0", "intval", "bool"),
			"dely" => array("POST", "0", "intval", "bool"),
			"msg" => array("POST", "0", "intval", "bool"),
			"user" => array("POST", "0", "intval", "bool"),
			"log" => array("POST", "0", "intval", "bool"),
			"adm" => array("POST", "0", "intval", "bool"),
		),
	);
	public $perm = array('adm', "index.php?action=login.tologin");

	function ShowList()
	{
		$obj = $this->GetModel("admin");
		$data = $obj->GetAdminList($this->params['page']);
		$this->assign("listdata", $data['data']);
		$this->assign("totalCnt", $data['cnt']);
		$this->assign("pageCnt", $data['pageCnt']);
		$this->assign("curPage", $this->params['page']);
		return RT_TPL;
	}

	function SetPwd()
	{
		$obj = $this->GetModel("admin");
		$id = $this->params['id'];
		$data = $obj->GetBaseInfo($id);
		if(!$data)
		{
			echo ADM_NEXIST;
			return RT_NONE;
		}
		$pwd = RandStr(8);
		$cnt = $obj->SetPwd($this->params['id'], $pwd);
		echo "0";
		$this->CoreLog('adm_pwd', $id, $cnt, '');
		$retmsg;
		SendMobileMsg($retmsg, array($data['mobile']), ADM_PWDRST. "[$pwd]" . SAFE_NOTICE);
		return RT_NONE;
	}

	function Info()
	{
		$obj = $this->GetModel("admin");
		$data = $obj->GetInfo($this->params['id']);
		if(!$data)
		{
			$this->assign("msg", "用户不存在");
			return RT_MSG;
		}
		$this->assign("data", $data);
		return RT_TPL;
	}

	function Add()
	{
		$data = array(
			'name' => $this->params['username'],
			'password' => md5($this->params['password']),
			'mobile' => $this->params['mobile'],
			'email' => $this->params['email'],
			'truename' => $this->params['truename'],
			'desp' =>$this->params['desp'],
			'p_cake' => $this->params['cake'],
			'p_order' => $this->params['order'],
			'p_prod' => $this->params['prod'],
			'p_dely' => $this->params['dely'],
			'p_msg' => $this->params['msg'],
			'p_adm' => $this->params['adm'],
			'p_log' => $this->params['log'],
			'p_user' => $this->params['user'],
		);
		$obj = $this->GetModel("admin");
		$id = $obj->AddAdmin($data);
		if($id == 0)
		{
			$this->assign("msg", "添加失败, 请检查参数");
			$this->assign("alert", "电话、账号、真实姓名、邮箱有重复。");
		}
		else
		{
			$info = "cake=" . $this->params['cake'] . ",order=" . $this->params['order'] . ',prod=' . $this->params['prod']
				. ',dely=' . $this->params['dely'] . ',msg=' . $this->params['msg']
				. ',user=' . $this->params['user'] . ',log=' . $this->params['log'] . ',adm=' . $this->params['adm'];
			$this->CoreLog('adm_add', $id, 0, $info);
			$this->assign("msg", "添加成功,ID:$id");
		}
		return RT_MSG;
	}

	function SetPerm()
	{
		$data = array(
			'p_cake' => $this->params['cake'],
			'p_order' => $this->params['order'],
			'p_prod' => $this->params['prod'],
			'p_dely' => $this->params['dely'],
			'p_msg' => $this->params['msg'],
			'p_adm' => $this->params['adm'],
			'p_log' => $this->params['log'],
			'p_user' => $this->params['user'],
		);

		$obj = $this->GetModel("admin");
		$id = $this->params['id'];
		$cnt = $obj->SetPerm($id, $data);
		if($cnt == 0)
			echo "2";
		else
		{
			echo "0";
			$info = "cake=" . $this->params['cake'] . ",order=" . $this->params['order'] . ',prod=' . $this->params['prod']
				. ',dely=' . $this->params['dely'] . ',msg=' . $this->params['msg']
				. ',user=' . $this->params['user'] . ',log=' . $this->params['log'] . ',adm=' . $this->params['adm'];
			$this->CoreLog('adm_perm', $id, 0, $info);
		}
		return RT_NONE;
	}
}

